<?php
	if(!class_exists('class_manage'))
	{
		exit('Not find class Manager');
	}
	class class_sanpham extends class_manage{
	
		/*
		*| DuyK add and edited
		*| Function __construct()
		*/
		function __construct()
			{
			parent::__construct();
			$this->validation_xxx='xxx|sex|wtf';
			}
	
		/*
		*| Function list san pham, vip=1 => order theo vip
		*/
		function list_san_pham($page, $result, $vip=0 ){
				
				// Input filter
				$page = intval($page);
				$result = intval($result);
				
				// Build vip query
				if($vip!=0)
					{
					$vip = 1;
					$vip_query = "vip=".$vip;
					}
				else
					{
					$vip_query= "1=1";
					}
				
			// Query
			$start = ($page-1)*$result;
			$query="SELECT * FROM sanpham WHERE ".$vip_query." ORDER BY datepost DESC LIMIT $start,$result";
			$arr=array();
			$db=$this->data;
			$rl=$db->query($query);
			if($rl!=false){
				while($rw=$rl->fetch_assoc()){
					array_push($arr, $rw);
				}
			}
			return $arr;
		}
		
		function xem_san_pham($id){
			$id = intval($id);
			$query="select * from sanpham where id='$id'";
			$db=$this->data;
			$rl=$db->query($query);
			if($rl->num_rows==0){
				return false;
			}else{
				$result=$rl->fetch_assoc();
				return $result;
			}
		}
		
		function dang_san_pham($data, $upload_path = ''){
			if($upload_path!='')$this->upload_path=$upload_path;
			if($_FILES['minh_hoa']['name']!=''){
				$config['upload_path'] 		= 	$this->upload_path;
				$config['allowed_types'] 	= 	'gif|jpg|png';
				$config['max_size']			= 	8*1024;
				$config['max_width']  		= 	1024*100;
				$config['max_height']  		= 	1024*100;
				$config['max_filename']		=	250;
				$config['encrypt_name']		=	false;
				$upload = $this->load('class_upload_library');
				$upload->initialize($config);
				if($upload->do_upload('minh_hoa')){
					$data['image']=$upload->data();
					//********** config image here
					$data['image']=$this->upload_path.$data['image']['file_name'];
				}else{
					$data['run']=false;
					$data['log']=$upload->display_errors();
					return $data;
				}
			}else{
				$data['image']='';
			}
			$data['duyet']=0;
			if($data['link']!='' && $this->isValidURL($data['link'])!=false){
				$get_content=@file_get_contents($data['link']);
				if($get_content){
					if($this->validation_xxx(strip_tags($get_content))){
						$data['duyet']=1;
					}else{
						$data['duyet']=0;
					}
				}else{
					$data['link']='';
				}
			}else{
				$data['link']='';
			}
			$db=$this->data;
			$user=$this->load('class_member');
			$user=$user->user_info('user_id');
			return $db->insert('sanpham', array(
										'user_id'		=>	$user['user_id'],
										'tieu_de'		=>	strip_tags($data['tieu_de']),
										'minh_hoa'		=>	$data['image'],
										'noi_dung'		=>	$data['noi_dung'],
										'gia'			=>	intval(strip_tags($data['gia'])),
										'gia_thitruong'=>	intval(strip_tags($data['gia_thitruong'])),
										'noi_rao'		=>	strip_tags($data['noi_rao']),
										'main_cat'		=>	$data['main_cat'],
										'sub1'			=>	$data['sub1'],
										'sub2'			=>	$data['sub2'],
										'duyet'			=>	$data['duyet'],
										'link'			=>	$data['link'],
										'datepost'		=>	time(),
			));
			$db->close();
		}
		
			/*========================================/*
			*| Ham chinh sua san pham va lien ket
			*| input la id va 1 array ung voi cac column trong data
			*| Ham nay tra ve loi thoai tu language
			*| Array nhap vao co cac column nao thi se update column do trong database
			*| Key language su dung: sua_san_pham_thanh_cong , khong_phai_san_pham_cua_ban, input_sai
			/*========================================*/
			function sua_san_pham($id, $array)
				{
					if(is_array($array))
						{
							// Check so huu id
							$user_info = $this->load('class_member')->user_info('user_id');
								$id=intval($id);
							$shop_user = $this->data->query("SELECT user_id FROM sanpham WHERE id='$id'")->fetch_assoc();
							if($user_info['user_id']==$shop_user['user_id'])
								{
									foreach($array as $key => $value)
										{
											$value = addslashes($value);
											$set_value .= $key.'=\''.$value.'\',';
										}
									$set_value = substr($set_value, '', -1);
									// Update mysql
									$this->data->query("UPDATE sanpham SET ".$set_value." WHERE id='$id'");
									// Return text
									return $this->language_normal('sua_san_pham_thanh_cong');
								}
							else
								{
									return $this->language_normal('khong_phai_san_pham_cua_ban');
								}
						}
						return $this->language_normal('input_sai');
				}
		
		function validation_xxx($str){
			$arr_validation=explode('|', $this->validation_xxx);
			foreach($arr_validation as $val){
				if(strpos($str, $val)!=''){
					return false;
				}
			}
			return true;
		}
		
		function khongdau($str) {
		 	$unicode = array(
					'a'=>'á|à|ả|ã|ạ|ă|ắ|ặ|ằ|ẳ|ẵ|â|ấ|ầ|ẩ|ẫ|ậ',
					'd'=>'đ',
					'e'=>'é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ',
					'i'=>'í|ì|ỉ|ĩ|ị',
					'o'=>'ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ',
					'u'=>'ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự',
					'y'=>'ý|ỳ|ỷ|ỹ|ỵ',
					'A'=>'Á|À|Ả|Ã|Ạ|Ă|Ắ|Ặ|Ằ|Ẳ|Ẵ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ',
					'D'=>'Đ',
					'E'=>'É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ',
					'I'=>'Í|Ì|Ỉ|Ĩ|Ị',
					'O'=>'Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ',
					'U'=>'Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự',
					'Y'=>'Ý|Ỳ|Ỷ|Ỹ|Ỵ',
					'-'=>' |\'|"|_|&',
				);
				
				$str = preg_replace('~\s+~', ' ', trim($str));
			   	foreach($unicode as $nonUnicode=>$uni){
					$str = preg_replace("/($uni)/i", $nonUnicode, $str);
			   	}
					$str = str_replace('"', '-', $str);
					$str = str_replace('”', '-', $str);
					$str = str_replace('“', '-', $str);
					$str = str_replace(',', '-', $str);
					$str = str_replace('/', '-', $str);
					$str = str_replace('*', '-', $str);
					$str = str_replace('(', '-', $str);
					$str = str_replace(')', '-', $str);
					$str = str_replace(':', '-', $str);
					$str = str_replace(';', '-', $str);
					$str = str_replace(',', '-', $str);
					$str = str_replace('+', '-', $str);
					$str = str_replace('<', '-', $str);
					$str = str_replace('>', '-', $str);
					$str = str_replace('?', '-', $str);
					$str = str_replace('!', '-', $str);
					$str = str_replace('@', '-', $str);
					$str = str_replace('#', '-', $str);
					$str = str_replace('$', '-', $str);
					$str = str_replace('%', '-', $str);
					$str = str_replace('^', '-', $str);
					$str = str_replace('=', '-', $str);
					$str = str_replace('|', '-', $str);
					$str = str_replace('~', '-', $str);
					$str = str_replace('`', '-', $str);
		
					$str = str_replace('--', '-', $str);
					$str = str_replace('--', '-', $str);
					$str = str_replace('--', '-', $str);
				if(substr($str, strlen($str)-1,1)=='-'){
					$str=substr($str,0,strlen($str)-1);
				}
				return strtolower($str);
		}
		
		function isValidURL($url)
		{
			return preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url);
		}
		
		function post($index = NULL)
		{
			// Check if a field has been provided
			if ($index === NULL AND ! empty($_POST))
			{
				$post = array();
	
				// Loop through the full _POST array and return it
				foreach (array_keys($_POST) as $key)
				{
					$post[$key] = $this->_fetch_from_array($_POST, $key);
				}
				return $post;
			}
	
			return $this->_fetch_from_array($_POST, $index);
		}
		
		
		function _fetch_from_array(&$array, $index = '')
		{
			if ( ! isset($array[$index]))
			{
				return FALSE;
			}
	
			return $array[$index];
		}
	}
?>